package cn.itcast.order.service;


import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.Order;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 远程查询user信息
//        String url = "http://localhost:8081/user/" + order.getUserId();
        // 使用了负载均衡之后,服务之间的调用不用采用套接字而是服务名的形式访问,这样可以防止用户直接访问
//        String url = "http://userservice/user/" + order.getUserId();
//        User user = restTemplate.getForObject(url,User.class);
        // 使用feign客户端实现远程链接
        User user = userClient.findById(order.getUserId());
        // 装填数据
        order.setUser(user);
        // 4.返回
        return order;
    }
}
